package com.health.db.dao.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IVoiceOnDemondDao;
import com.health.db.mapper.VoiceOnDemondMapper;
import com.health.db.model.VoiceOnDemond;
import com.health.db.model.VoiceOnDemondExample;
import com.health.redis.RedisConstants;


/**
 * 孙奶奶音频
 * 缓存适用
 * @author YC
 */
public class VoiceOnDemondDaoImpl extends ServiceInterface implements
		IVoiceOnDemondDao {

	VoiceOnDemondMapper voiceOnDemondMapper;

	/**
	 * example object
	 * {
	 * stuffId  医生id 可以为空 
	 * }
	 */
	@Override
	public List<VoiceOnDemond> selectByExample(VoiceOnDemondExample example) {
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.VoiceOnDemondMapper ;
		
		if (null!=example.getStuffId()) {
			
			key=key+":stuffId:"+example.getStuffId();
		}else if (null!=example.getRecid()) {
			key=key+":recId:"+example.getRecid();
		}
		Object object = null;
		List<VoiceOnDemond> list = new ArrayList<VoiceOnDemond>();
		if (redisOperatiorLogic.isJedisIsConnected()) {
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}

		if (null != object ) {
			if (!object.equals("")) {
				list = (List<VoiceOnDemond>) object;
			}
		} else {
		 
			list = voiceOnDemondMapper.selectByExample(example);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,	getOrderRedisGroupNo("1"));
			}
		}
		return list;
		
	}

	public VoiceOnDemondMapper getVoiceOnDemondMapper() {
		return voiceOnDemondMapper;
	}

	public void setVoiceOnDemondMapper(VoiceOnDemondMapper voiceOnDemondMapper) {
		this.voiceOnDemondMapper = voiceOnDemondMapper;
	}

	/**
	 * recid 主键
	 */
	@Override
	public VoiceOnDemond selectByPrimaryKey(Long recid) {
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.VoiceOnDemondMapper  
					+":recid:"+recid;
		Object object = null;
		VoiceOnDemond list = new VoiceOnDemond();
		if (redisOperatiorLogic.isJedisIsConnected()) {
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}
		if (null != object ) {
			if (!object.equals("")) {
			list = (VoiceOnDemond) object;
			}
		} else {
		 
			list = voiceOnDemondMapper.selectByPrimaryKey(recid);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,	getOrderRedisGroupNo("1"));
			}
		}
		return list;
		
	}

	@Override
	public List<VoiceOnDemond> selectBySimple(VoiceOnDemondExample example) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.VoiceOnDemondMapper+"SP" ;
		
		if (null!=example.getStuffId()) {
			key=key+":stuffId:"+example.getStuffId();
		}else if (null!=example.getRecid()) {
			key=key+":recId:"+example.getRecid();
		}
		Object object = null;
		List<VoiceOnDemond> list = new ArrayList<VoiceOnDemond>();
		if (redisOperatiorLogic.isJedisIsConnected()) {
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}

		if (null != object ) {
			if (!object.equals("")) {
				list = (List<VoiceOnDemond>) object;
			}
		} else {
			list = voiceOnDemondMapper.selectBySimple(example);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,	getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

}
